package main;

import game.Game;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.image.MemoryImageSource;
import java.io.File;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.Timer;

/*
 * Instructions.java
 *
 * Created on May 2, 2009, 11:18:55 PM
 */

/**
 *
 * @author Vinay
 */
public class Instructions extends javax.swing.JInternalFrame {

    /** Creates new form Instructions */
    java.net.URL docUrl;
    Game g = new Game();
    String strPage="";
	MouseListener listener;

    int totalTime=60,secs = totalTime, timeTaken=0;
    Timer time = new Timer(1000, new TimeListener());
    final int DEBUG_TIME = 2;

    public Instructions() {
         super("",
              false, //resizable
              false, //closable
              false, //maximizable
              false);//iconifiable
        javax.swing.plaf.InternalFrameUI ui = this.getUI();
        ((javax.swing.plaf.basic.BasicInternalFrameUI)ui).setNorthPane(null);

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        initComponents();
        
        setInst("Centipede");
    }

    public Instructions(String game) {
         super("",
              false, //resizable
              false, //closable
              false, //maximizable
              false);//iconifiable
        javax.swing.plaf.InternalFrameUI ui = this.getUI();
        ((javax.swing.plaf.basic.BasicInternalFrameUI)ui).setNorthPane(null);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        initComponents();

        if (g.debugMode) {
            totalTime = DEBUG_TIME;
            secs = totalTime;
        }

		listener = new Instructions.CheckMouse();
		g.runner.addMouseListener (listener);
        addMouseListener (listener);
		this.getGlassPane().addMouseListener (listener);

        int min = secs/60;
        int sec = secs%60;
        lblTime.setText(min+":"+((sec<10)?"0":"")+sec);
        time.start();

        setInst(game);
        this.setVisible(true);
        hideMouse();
    }

	class CheckMouse implements MouseListener {
		public void mousePressed(MouseEvent e) { hideMouse(); }
        public void mouseReleased(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
        public void mouseClicked(MouseEvent e) {	
			int modifiers = e.getModifiers();
			if ((modifiers & InputEvent.BUTTON3_MASK)
					== InputEvent.BUTTON3_MASK) {
				endInstructions();
			}
		}
	}

	private void endInstructions() {
		time.stop();
		try {
			this.getGlassPane().setVisible (false);
			this.getGlassPane().removeMouseListener (listener);
			g.runner.removeMouseListener (listener);
			removeMouseListener (listener);
			setVisible(false);
			g.free=true;
		} catch(Exception ex) {
			ex.printStackTrace();
			g.logError(ex);
		}
	}

    private void setInst(String game) {
        try {
            File f = new File("instructions/"+game+".html");
            docUrl = f.toURL();
//            strPage = f.getAbsolutePath();
//            docUrl = this.getClass().getResource("/instructions/"+game+".html");
            jEdInst.setPage(docUrl);
//            jEdInst.setPage(strPage);
        }
        catch(Exception ex) {
            ex.printStackTrace();
            g.logError(ex);
        }
    }

    private void hideMouse() {
        int[] pixels = new int[16 * 16];
        Image image = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(16, 16, pixels, 0, 16));
        Cursor transparentCursor = Toolkit.getDefaultToolkit().createCustomCursor(image, new Point(0, 0), "invisibleCursor");
		this.getGlassPane().setCursor(transparentCursor);
		this.getGlassPane().setVisible (true);
        g.runner.setCursor(transparentCursor);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jEdInst = new javax.swing.JEditorPane();
        lblTime = new javax.swing.JLabel();
        lblNote = new javax.swing.JLabel();
        jLayeredPane1 = new javax.swing.JLayeredPane();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(null);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(800, 600));

        jEdInst.setBorder(null);
        jEdInst.setEditable(false);
        jEdInst.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jEdInstMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jEdInst);

        lblTime.setFont(new java.awt.Font("Calibri", 1, 36)); // NOI18N
        lblTime.setForeground(new java.awt.Color(153, 102, 0));
        lblTime.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/hrGlass.png"))); // NOI18N
        lblTime.setText("3:00");
        lblTime.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblTimeMouseClicked(evt);
            }
        });

        lblNote.setFont(new java.awt.Font("Calibri", 1, 24)); // NOI18N
        lblNote.setForeground(new java.awt.Color(0, 102, 204));
        lblNote.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblNote.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblNoteMouseClicked(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 28)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("SKIP");
        jLabel2.setBounds(30, 9, 230, 70);
        jLayeredPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/greenBtnSmall.png"))); // NOI18N
        jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel1MouseClicked(evt);
            }
        });
        jLabel1.setBounds(30, 0, 240, 90);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE)
                    .add(lblTime, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(lblNote, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 503, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLayeredPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 273, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(lblTime, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 48, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 404, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLayeredPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                    .add(lblNote, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void jEdInstMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jEdInstMouseClicked
		int modifiers = evt.getModifiers();
		if ((modifiers & InputEvent.BUTTON3_MASK)
				== InputEvent.BUTTON3_MASK) {
			 endInstructions();
		}
	}//GEN-LAST:event_jEdInstMouseClicked

	private void lblNoteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblNoteMouseClicked
		int modifiers = evt.getModifiers();
		if ((modifiers & InputEvent.BUTTON3_MASK)
				== InputEvent.BUTTON3_MASK) {
			 endInstructions();
		}
	}//GEN-LAST:event_lblNoteMouseClicked

	private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel1MouseClicked
		int modifiers = evt.getModifiers();
		if ((modifiers & InputEvent.BUTTON3_MASK)
				== InputEvent.BUTTON3_MASK) {
			 endInstructions();
		}
	}//GEN-LAST:event_jLabel1MouseClicked

	private void lblTimeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblTimeMouseClicked
		int modifiers = evt.getModifiers();
		if ((modifiers & InputEvent.BUTTON3_MASK)
				== InputEvent.BUTTON3_MASK) {
			 endInstructions();
		}
	}//GEN-LAST:event_lblTimeMouseClicked

    class TimeListener implements ActionListener {
    	public void actionPerformed(ActionEvent e) {
    		secs--;
            if(secs>=0) {
                int min = secs/60;
                int sec = secs%60;
                lblTime.setText(min+":"+((sec<10)?"0":"")+sec);
            }
            if(secs==6) {
                lblNote.setText("Starting the Quiz in a few moments ...");
            }
            if(secs==0) {
                time.stop();
                endInstructions();
            }
    	}

    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        JFrame main = new JFrame();
        JDesktopPane dPane = new JDesktopPane();
        Instructions frame = new Instructions("Impulse");
        main.setSize(820, 620);
        dPane.add(frame);
        main.add(dPane);
        main.setVisible(true);
        dPane.setVisible(true);
        frame.setVisible(true);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JEditorPane jEdInst;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblNote;
    private javax.swing.JLabel lblTime;
    // End of variables declaration//GEN-END:variables

}
